package utilities;


import java.util.*;
import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

public class ProcessorCommander implements Commander{
	
	private BufferedWriter log = null;
	private String name;
	private Processor p;
	private List workQueue;
	private Map filterDefs;
	private String className;
	private int sleeps = 1000;
	private String note;
	private String configFile;
	private String appDir;
	private Document doc;

	private  String outFileName=null;
	private  String scanDelay=null; 
	private  String debugLevel=null;
	private  String active = null;
	private  String engineAppDir;

	
	private Object configData;
	              
	public ProcessorCommander(String name, String note, String appDir, String configFile)
	{
		this.name = name;
		this.configFile = configFile;
		this.note = note;		
		this.appDir = appDir;
		
		this.processConfig();
		
		//create the log file.
		try {
			log = new BufferedWriter(new FileWriter(outFileName));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
		this.loadClassInstance();
		
		
		p.newProcessor(name, log, workQueue, this,
				Integer.valueOf(scanDelay).intValue(),
				Integer.valueOf(debugLevel).intValue());
	    p.start();		
		
	    try {
			log.write(name + " started\n");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	   
	}
	
	public void close()
	{
		try {
			p.stop();
			p.exit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			log.write(name + " stopped\n");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		try {
			log.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	private void loadClassInstance()
	{
	    ClassLoader classLoader = ProcessorCommander.class.getClassLoader();	    
	    try {
	        Class aClass = classLoader.loadClass(className);

	        p = (Processor) aClass.newInstance();

	    } catch (Exception e) {
	        e.printStackTrace();
	    }	
	}
	

	
	public void restart()
	{
		this.kill();
		processConfig();

		p.newProcessor(name, log, workQueue, this, 
				Integer.valueOf(scanDelay).intValue(),Integer.valueOf(debugLevel).intValue());
		try {
			log.write(name + " restarted\n");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    p.start();		
		
	}
	
	public void deActivate()
	{
		p.deActivate();
	}
	public void activate()
	{
		p.activate();
	}
	
	public void kill()
	{
		p.destroy();
		p=null;
	}
	public int getQueueCnt()
	{
		return workQueue.size();		
	}
	public void addKey(String key)
	{
		//t/ry {
			//log.write("proc commander added: " + key + "\n");
		//} catch (IOException e) {
			/// TODO Auto-generated catch block
			//e.printStackTrace();
		//}
		workQueue.add(key);
	}
	public void clearQueue()
	{
		workQueue.clear();
	}
	public KeyActions getFilterByName(String name)
	{
		if (filterDefs.containsKey(name)) return (KeyActions) filterDefs.get(name);		
		return null;
	}
	
	public void processConfig()
	{
		sleeps = 1000;
		this.workQueue = new ArrayList();		
		
		//create config data.
		String fName = appDir + this.configFile;
		
		  try {
		  File file = new File(fName);
		  
		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		  DocumentBuilder db = dbf.newDocumentBuilder();
		  Document doc = db.parse(file);
		  doc.getDocumentElement().normalize();
		  		  
		  /*
		   * Parameters section
		   */
		  NodeList nodeLst = doc.getElementsByTagName("params");  
			
		 

		  nodeLst = doc.getElementsByTagName("outFileName");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  	Node tn = nodeLst.item(s);
			  	outFileName = tn.getChildNodes().item(0).getNodeValue();
			  	//tn.getChildNodes().item(0).setNodeValue("TestOutValue");
			  	break;
		  }
		  
		  nodeLst = doc.getElementsByTagName("appDir");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  	Node tn = nodeLst.item(s);
			  	engineAppDir = tn.getChildNodes().item(0).getNodeValue();
			  	//tn.getChildNodes().item(0).setNodeValue("TestOutValue");
			  	break;
		  }
		  
		  nodeLst = doc.getElementsByTagName("scanDelay");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  scanDelay = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
			    break;
		  }
		  nodeLst = doc.getElementsByTagName("debugLevel");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  debugLevel = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
			    break;
		  }
		  nodeLst = doc.getElementsByTagName("active");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  active = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
			    break;
		  } 
		  
		  nodeLst = doc.getElementsByTagName("className");
		  for (int s = 0; s < nodeLst.getLength(); s++) {
			  className = nodeLst.item(s).getChildNodes().item(0).getNodeValue();
			    break;
		  } 
		  
		  
		  
		  //load actions.....
		  filterDefs = new TreeMap();
		  nodeLst = doc.getElementsByTagName("filter");
		  String pk = null;

		  
		  for (int s = 0; s < nodeLst.getLength(); s++) {
		
			  Node fstNode2 = nodeLst.item(s);
			  Element fstElmnt2 = (Element) fstNode2;
			  
			  NodeList nodeLst2 = fstElmnt2.getElementsByTagName("key");
			  for (int s2 = 0; s2 < nodeLst.getLength(); s2++) {
				  pk = nodeLst2.item(s2).getChildNodes().item(0).getNodeValue();
				    break;
			  } 			  
			  
			  
			  //here w want to pull all nodes and create KeyActions 
			  //from nodelist......
			  nodeLst2 = fstElmnt2.getElementsByTagName("action");			  
			  KeyActions ka = new KeyActions(pk,nodeLst2);
			  this.filterDefs.put(pk, ka);
		  } 
		  
		  
		 		  
		  
		  } catch (Exception e) {
		    e.printStackTrace();
		  }
		  
		  
		  
		  
		 		
	}
	
	
}